package org.example.JUC.Future;

import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;

/*创造线程的两种方式*/

public class CallableTool {
    public interface ICallback<T>{
        public <T> T doSomeThing() throws Exception;
    }

    public static <T> Callable<T> createCallable(final ICallback<T> callback){
        return new Callable<T>() {
            @Override
            public T call() throws Exception {
                return callback.doSomeThing();
            }
        };
    }

    public static <T>FutureTask<T> createFutureTask(final ICallback<T> callback){
        FutureTask<T> task=new FutureTask<>(new Callable<T>() {
            @Override
            public T call() throws Exception {
                return callback.doSomeThing();
            }
        });
        return task;
    }

    public static <T> Thread createThread(String name,FutureTask<T> task,boolean isDaemon){
        Thread thread=new Thread(task,name);
        thread.setDaemon(isDaemon);
        return thread;
    }


}
